const { Group } = require('../database');

module.exports = async (ctx) => {
  const group = await Group.findOne({ group_id: ctx.chat.id });
  const rule = group.rules;
  if (!group) {
    try {
      const answer = ctx.i18n.t('group.notfound');
      ctx.replyWithMarkdown(answer,
        { reply_to_message_id: ctx.message.message_id });
    } catch (error) {
      const answer = ctx.i18n.t('error', { error });
      ctx.replyWithMarkdown(answer,
        { reply_to_message_id: ctx.message.message_id });
    }
  } else if (rule !== '') {
    try {
      ctx.reply(rule,
        { reply_to_message_id: ctx.message.message_id });
    } catch (error) {
      const answer = ctx.i18n.t('error', { error });
      ctx.replyWithMarkdown(answer,
        { reply_to_message_id: ctx.message.message_id });
    }
  } else {
    try {
      const answer = ctx.i18n.t('rule.err');
      ctx.reply(answer,
        { reply_to_message_id: ctx.message.message_id });
    } catch (error) {
      const answer = ctx.i18n.t('error', { error });
      ctx.replyWithMarkdown(answer,
        { reply_to_message_id: ctx.message.message_id });
    }
  }
};
